<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@page import="cn.wh.xwm.hibernate.dao.CustomerinfoDAO"%>
<%@page import="java.util.List"%>
<%@page import="cn.wh.xwm.hibernate.dao.Customerinfo"%>
<%@page import="cn.wh.xwm.hibernate.dao.Userinfo"%>
<%@page import="cn.wh.xwm.hibernate.dao.UserCustomerRelationDAO"%>
<%@page import="cn.wh.xwm.hibernate.dao.UserCustomerRelation"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>赛诺菲_西南区 销量统计系统</title>
		<%@ include file="../inc/fileHead.jsp" %>
	</head>
	<body class="easyui-layout">
	<div data-options="region:'center',border:false" style="height:100%;">
		<table id="dg" title="销量录入" class="easyui-datagrid"
			url="SalesAction_findAllSales"
			toolbar="#toolbar" pagination="true" rownumbers="true"
			fitColumns="true" singleSelect="true">
			<thead>
				<tr>
					<th field="eventDate" width="50">
						销售日期
					</th>
					<th field="customerFromName" width="50">
						供货商
					</th>
					<th field="customerToName" width="50">
						接种点
					</th>
					<th field="productName" width="50">
						产品名称
					</th>
					<th field="amount" width="50">
						数量（支）
					</th>
					<th field="patchNumber" width="50">
						批号
					</th>
				</tr>
			</thead>
		</table>
		<div id="toolbar">
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-add" plain="true" onclick="newUser()">记录销量</a>
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editUser()">修改销量</a>
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-remove" plain="true" onclick="destroyUser()">删除销量</a>
		</div>

		<div id="dlg" class="easyui-dialog"
			style="width: 600px; height: 350px; padding: 10px 20px" closed="true"
			buttons="#dlg-buttons">
			<div class="ftitle">
				销售记录
			</div>
			<form id="fm" method="post" novalidate>
			<div class="fitem">
					<label>
						供货商
					</label>
					<select name="customerFromId" class = "easyui-combobox" required="true" style="width: 400px">
						<% 
							CustomerinfoDAO cdao = new CustomerinfoDAO();
							List<Customerinfo> list = cdao.findAll();
							for(Customerinfo info:list){
							 
						%>
							<option value="<%=info.getId() %>"><%=info.getName() %></option>
						<%
							}
						 %>
					</select>
					<!-- 
					<input class="easyui-combobox" name="customerFromId" required="true" style="width: 400px"
						data-options="  
                    url:'BaseAction_selectGetCustomerName',     
                    valueField:'id',  
                    textField:'name',  
                    panelHeight:'auto'  
            ">
					 -->
				</div>
				<div class="fitem">
					<label>
						接种点
					</label>
					<select name="customerToId" class = "easyui-combobox" required="true" style="width: 400px">
						<% 
							Userinfo userinfo = (Userinfo)session.getAttribute("USER_SESSION");
							UserCustomerRelationDAO userCustomerRelationDAO =  new UserCustomerRelationDAO();
							List<UserCustomerRelation> relationlist = userCustomerRelationDAO.findByProperty("id.userId", userinfo.getUserid());
							for(UserCustomerRelation info :relationlist){
								Customerinfo cinfo = cdao.findById(info.getId().getCustomerId());
								%>
							<option value="<%=cinfo.getId() %>"><%=cinfo.getName() %></option>
						<%
							}
							cdao.getSession().close();
						%>
					</select>
					<!-- 
					<input class="easyui-combobox" name="customerToId" required="true" style="width: 400px"
						data-options="  
                    url:'BaseAction_selectGetCustomerNameBySalesId',  
                    valueField:'id',  
                    textField:'name',  
                    panelHeight:'auto'  
            ">
					 -->
				</div>
				<div class="fitem">
					<label>
						销售日期
					</label>
					<input id="eventDate" type="text" name="eventDate" required="true"></input>
				</div>
				<div class="fitem">
					<label>
						产品名称
					</label>
					<input class="easyui-combobox" name="productId" required="true"
						data-options="  
                    url:'BaseAction_selectGetProduct',  
                    valueField:'id',  
                    textField:'name',  
                    panelHeight:'auto'  
            ">
				</div>
				<div class="fitem">
					<label>
						数量（支）
					</label>
					 <input class="easyui-numberbox" required="true" name="amount"/>  
				</div>
				<div class="fitem">
					<label>
						批号
					</label>
					<input name="patchNumber" class="easyui-validatebox"
						required="true">
				</div>
			</form>
		</div>
		<div id="dlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-ok" onclick="saveUser()">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
		</div>
		<script type="text/javascript">
		
			$('#eventDate').datebox({  
    			required:true,
    			formatter: function(date){
					var y = date.getFullYear();
					var m = date.getMonth()+1;
					var d = date.getDate();
					return   y+'-'+m+'-'+d;
				} 
			});  
            var url;  
            function newUser(){  
                $('#dlg').dialog('open').dialog('setTitle','添加销量');  
                $('#fm').form('clear');  
                url = 'SalesAction_addData';  
            }  
            function editUser(){  
                var row = $('#dg').datagrid('getSelected');  
                if (row){  
                    $('#dlg').dialog('open').dialog('setTitle','修改销量');  
                    $('#fm').form('load',row);  
                    url = 'SalesAction_updateData?id='+row.id;  
                }  
            }  
            function saveUser(){  
                $('#fm').form('submit',{  
                    url: url,  
                    onSubmit: function(){  
                        return $(this).form('validate');  
                    },  
                    success: function(result){  
                        var result = eval('('+result+')');  
                        if (result.errorMsg){  
                           $.messager.alert('错误',result.errorMsg); 
                        } else {  
                            $('#dlg').dialog('close');      // close the dialog  
                            $('#dg').datagrid('reload');    // reload the user data  
                        }  
                    }  
                });  
            }  
            function destroyUser(){  
                var row = $('#dg').datagrid('getSelected');  
                if (row){  
                    $.messager.confirm('Confirm','确定要删除该销量?',function(r){  
                        if (r){  
                            $.post('SalesAction_deleteData',{id:row.id},function(result){  
                                if (result.success){  
                                    $('#dg').datagrid('reload');    // reload the user data  
                                } else {  
                                    $.messager.alert('错误',result.errorMsg); 
                                }  
                            },'json');  
                        }  
                    });  
                }  
            }  
        </script>
		<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>
</div>
	</body>
</html>